package ai.turbochain.ipex.service;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ai.turbochain.ipex.constant.OrderStatus;
import ai.turbochain.ipex.dao.MemberDao;
import ai.turbochain.ipex.dao.OrderDao;
import ai.turbochain.ipex.entity.QMember;
import ai.turbochain.ipex.entity.Statistics;
import ai.turbochain.ipex.service.Base.BaseService;
import ai.turbochain.ipex.util.DateUtil;
import ai.turbochain.ipex.util.EnumHelperUtil;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jack
 * @description 统计service
 * @date 2020/1/8 16:21
 */
@Service
public class StatisticsService extends BaseService {
    @PersistenceContext
    private EntityManager em;

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    /**
     * @param sql 需要包含startTime endTime 两个占位符
     * @author jack
     * @description 获取统计数据
     * @date 2020/1/9 15:25
     */
    public List getStatistics(String startTime, String endTime, String sql) {
        Query query = em.createNativeQuery(sql);
        query.setParameter("startTime", startTime);
        query.setParameter("endTime", endTime);
        List resultList = query.getResultList();
        return resultList;
    }

    /**
     * 统计最近几日注册人数
     * @param day
     * @return
     */
    public  int getLatelyRegMember(int day){
        Date startTime = DateUtil.strToDate(DateUtil.getPastDate(day)+" 00:00:00");
        Date endTime = DateUtil.strToDate(DateUtil.getDate()+" 23:59:59");
        return memberDao.countByRegistrationTimeBetween(startTime,endTime);
    }

    /**
     * 查询时间段内订单数量 status为-1代表所有的状态
     * @param startTime
     * @param endTime
     * @param status
     * @return
     */
    public  int getLatelyOrder(String startTime,String endTime,int status){
        Date startTimeDate = DateUtil.strToDate(startTime +" 00:00:00");
        Date endTimeDate = DateUtil.strToDate(endTime +" 23:59:59");
        if(status<0){
            return orderDao.countByCreateTimeBetween(startTimeDate,endTimeDate);
        }
        OrderStatus orderStatus = EnumHelperUtil.indexOf(OrderStatus.class,status);
        return orderDao.countByStatusAndCreateTimeBetween(orderStatus,startTimeDate,endTimeDate);
    }
    //根据状态统计订单
    public  int getLatelyOrder(OrderStatus status){
        return orderDao.countByStatus(status);
    }
    public  int getLatelyAdvertise(int i){

        return 0;
    }
}
